#include <iostream>
#include <initializer_list>
/*
initializer_list是c++11新增的，-std=c++11
vector<double>的其中一个构造函数的参数就是initializer_list<double>
std::vector<double> p({34.0, 1.0, 2.0, 3.0})   <<< 这样写也是可以的

std::vector<double> p(10); 有10个元素的vector
std::vector<double> p({10}); 只有一个元素10

*/
double sum(std::initializer_list<double> list);
double average(const std::initializer_list<double> list);

double sum(std::initializer_list<double> list)
{
    double s = 0;
    for(auto p = list.begin(); p != list.end(); p++)
        s += *p;
    return s;
}
double average(const std::initializer_list<double> list)
{
    double s = 0;
    int n = list.size();
    for(auto p = list.begin(); p != list.end(); p++)
        s += *p;
    return s / n;
}

int main()
{
    using namespace std;
    cout << "sum = " << sum({1.0, 2.0, 3.0}) << ", avg = " << average({1.0, 2.0, 3.0}) << endl;
    initializer_list<double> list = {1.0, 2.0, 3.0};
    cout << "sum = " << sum(list) << ", avg = " << average(list) << endl;

    

    return 0;
}